package se.energimyndigheten.lampguiden.info;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import se.energimyndigheten.lampguiden.MainActivity;
import se.energimyndigheten.lampguiden.R;

public class InfoWebFragment extends android.support.v4.app.Fragment {

    public static final String EXTRA_VIEW = "view";
    public static final String EXTRA_MENUSELECTION = "menuSelection";
    public static final String EXTRA_TITLE = "title";

    private static final String TAG = InfoWebFragment.class.getSimpleName();

    private int mInfoId;
    private WebView mWebView;
    private int mSelectedActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.webview_activity, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Bundle args = getArguments();
        try {
            mInfoId = args.getInt(EXTRA_VIEW);
            mSelectedActivity = args.getInt(EXTRA_MENUSELECTION, -1);
            String title = args.getString(EXTRA_TITLE);
            ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(title);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
        mWebView = (WebView) getActivity().findViewById(R.id.webview);
        loadHtmlFile();
    }

    private String urlInfo(String tail) {
        return "file:///android_asset/submodule_html/Information/" + tail;
    }

    private String urlBest(String tail) {
        return "file:///android_asset/submodule_html/Basta_ljuset/" + tail;
    }

    private void loadHtmlFile() {
        if (mSelectedActivity == MainActivity.ACTIVITY_LIGHTING) {
            switch (mInfoId) {
                case R.string.lighting_bathroom:
                    mWebView.loadUrl(urlBest("sv_Badrum.html"));
                    break;
                case R.string.lighting_kitchen:
                    mWebView.loadUrl(urlBest("sv_Kok.html"));
                    break;
                case R.string.lighting_livingroom:
                    mWebView.loadUrl(urlBest("sv_Vardagsrum.html"));
                    break;
                case R.string.lighting_sleepingroom:
                    mWebView.loadUrl(urlBest("sv_Sovrum.html"));
                    break;
                case R.string.lighting_workingroom:
                    mWebView.loadUrl(urlBest("sv_Arbetsrum.html"));
                    break;
                default:
                    mWebView.loadUrl("file::///android_asset/404.html");
            }
        } else {
            switch (mInfoId) {
                case R.string.info_types:
                    mWebView.loadUrl(urlInfo("sv_Beskrivning_av_lamptyper.html"));
                    break;
                case R.string.info_recycling:
                    mWebView.loadUrl(urlInfo("sv_Atervinning_och_miljo.html"));
                    break;
                case R.string.info_definitions:
                    mWebView.loadUrl(urlInfo("sv_Ord_och_begrepp.html"));
                    break;
                case R.string.info_why_cancel:
                    mWebView.loadUrl(urlInfo("sv_Varfor_forsvann_glodlampan_ut.html"));
                    break;
                case R.string.info_legal:
                    mWebView.loadUrl(urlInfo("sv_Konsumentratt.html"));
                    break;
                case R.string.info_package:
                    mWebView.loadUrl(urlInfo("sv_Vad_star_pa_forpackningen.html"));
                    break;
                case R.string.info_about:
                    mWebView.loadUrl(urlInfo("sv_Om_Lampguiden.html"));
                    break;
                case R.string.info_energimyndigheten:
                    mWebView.loadUrl(urlInfo("sv_Om_Energimyndigheten.html"));
                    break;
                case R.string.info_energy:
                    mWebView.loadUrl(urlInfo("sv_Energiradgivare.html"));
                    break;
                default:
                    mWebView.loadUrl("file::///android_asset/404.html");
            }
        }
    }
}
